﻿using System;
using System.Collections.Generic;
using System.Linq;
using HappyBa.Core.Interfaces;

namespace HappyBa.Core.Data
{
    public class UIEntityBase : NamedBase
    {
        public string Description { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
        public DateTime? ModifyDate { get; set; }
        public DateTime? LastCommentDate { get; set; }
        public EntityType EntityType { get; set; }
        public User Owner { get; set; }
        public IList<CustomField> CustomFields { get; set; }
        public override string ToString()
        {
            return EntityType + ": " + Id + ": " + Name;
        }

        public UIEntityBase(EntityBase entity, ReceivedData data)
        {
            Description = entity.Description;
            EndDate = entity.EndDate;
            Id = entity.Id;
            LastCommentDate = entity.LastCommentDate;
            ModifyDate = entity.ModifyDate;
            Name = entity.Name;
            StartDate = entity.StartDate;
            EntityType = data.GetEntityType(entity.EntityType.Id);
            Owner = entity.Owner;
            CustomFields = (entity.CustomFields??new List<CustomField>()).ToList();
        }
    }

    public class UIStatebleEntityBase : UIEntityBase
    {
        public EntityState EntityState { get; set; }
        public IList<UIComment> Comments { get; set; }

        public UIStatebleEntityBase(StatebleEntityBase entity, ReceivedData data)
            :base(entity, data)
        {
            EntityState = entity.EntityState;
            Comments = data.GetComments(entity.Id)
                .Select(x=>new UIComment(x, data))
                .ToArray();
        }
    }

    public class UIAssignment : Base
    {
        public Role Role { get; set; }
        public User GeneralUser { get; set; }

        public UIAssignment(Assignment entity, ReceivedData data)
        {
            Role = data.GetRole(entity.Role.Id);
            GeneralUser = entity.GeneralUser;
        }

        public override string ToString()
        {
            return GeneralUser + " " + Role.Name;
        }
    }

    public class UIImpediment : UIStatebleEntityBase
    {
        public UIImpediment(Impediment entity, ReceivedData data) :
            base(entity, data)
        {
        }
    }

    public class UIEstimatableEntityBase : UIStatebleEntityBase
    {
        public decimal Effort { get; set; }
        public decimal EffortCompleted { get; set; }
        public decimal EffortToDo { get; set; }
        public decimal TimeSpent { get; set; }
        public decimal TimeRemain { get; set; }
        public DateTime? PlannedStartDate { get; set; }
        public DateTime? PlannedEndDate { get; set; }
        public Priority Priority { get; set; }
        public IList<UIAssignment> Assignments { get; set; }
        public IList<UIImpediment> Impediments { get; set; }

        public UIEstimatableEntityBase(EstimatableEntityBase entity, ReceivedData data)
            :base(entity, data)
        {
            Effort = entity.Effort;
            EffortCompleted = entity.EffortCompleted;
            EffortToDo = entity.EffortToDo;
            TimeSpent = entity.TimeSpent;
            TimeRemain = entity.TimeRemain;
            PlannedStartDate = entity.PlannedStartDate;
            PlannedEndDate = entity.PlannedEndDate;
            Priority = entity.Priority;
            EntityState = entity.EntityState;
            Assignments = data.GetAssignments(entity.Id)
                .Select(x=>new UIAssignment(x,data))
                .ToArray();
            Impediments = data.GetImpediments(entity.Id)
                .Select(x => new UIImpediment(x, data))
                .ToArray();
        }
    }

    public class UIBug : UIEstimatableEntityBase
    {
        public UIBug(Bug entity, ReceivedData data)
            : base(entity, data)
        {
            
        }
    }

    public class UITask : UIEstimatableEntityBase
    {
        public UITask(Task entity, ReceivedData data)
            : base(entity, data)
        {

        }
    }

    public class UIUserStory : UIEstimatableEntityBase
    {
        public IList<UIBug> Bugs { get; set; }
        public IList<UITask> Tasks { get; set; }
        public UIUserStory(UserStory entity, ReceivedData data)
            : base(entity, data)
        {
            Bugs = data.GetUserStoryBugs(entity.Id)
                .Select(x=>new UIBug(x,data))
                .ToArray();
            Tasks = data.GetTasks(entity.Id)
                .Select(x => new UITask(x, data))
                .ToArray();
        }
    }

    public class UIProject : UIEntityBase
    {
        public IList<UIBug> Bugs { get; set; }
        public IList<UIUserStory> UserStories { get; set; }

        public UIProject(Project entity, ReceivedData data)
            : base(entity, data)
        {
            Bugs = data.GetProjectBugs(entity)
                .Select(x => new UIBug(x, data))
                .ToArray();
            UserStories = data.GetUserStories(entity)
                .Select(x => new UIUserStory(x, data))
                .ToArray();
        }
    }

    public class UIComment : Base
    {
        public string Description { get; set; }
        public User Owner { get; set; }
        public DateTime CreateDate { get; set; }

        public UIComment(Comment entity, ReceivedData data)
        {
            Description = entity.Description;
            Owner = entity.Owner;
            CreateDate = entity.CreateDate;
        }
    }


}
